package com.hddf.project.auth.dao_impl;

import java.util.List;

import org.bson.types.ObjectId;

import com.hddf.project.auth.dao.AuthorityDao_MongoDB;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder ;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

public class AuthorityDaoImpl_MongoDB implements AuthorityDao_MongoDB {

	public DBObject getAuthById(String id, DBCollection c) {
		ObjectId oid = new ObjectId(id);
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("_id").is(oid).get();
		DBObject entity = c.findOne(query);
		return entity;
	}

	public List<DBObject> getAuthByIdArray(int[] array, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		//DBObject query = queryBuilder.put("user_id").is(userid).get();
		DBObject query = queryBuilder.put("auth_id").in(array).get();
		List<DBObject> list = c.find(query).toArray();
		//int[] arr = {1,7};
		//String[] arr = {"修改密码","用户管理3"};
		/*List<DBObject> list2 = c.find(new BasicDBObject("auth_id", 
				new BasicDBObject(QueryOperators.IN, array))).toArray();*/
		return list;
	}
	
	public List<DBObject> getAuthList(String start,String limit,int state, DBCollection c) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		if(state==1){
			state = 0;
		}else if(state==0){
			state = 1;
		}
		// notEquals 表示不等于 is 表示等于
		DBObject query = queryBuilder.put("state").notEquals(state).get();
		int st = 0;
		int li = 0;
		if(start!=null && limit!=null){
			st = Integer.parseInt(start);
			li = Integer.parseInt(limit);
		}
		//List<DBObject> list = c.find().toArray();
		// 1 代表正序 -1代表倒序  skip 代表从哪里开始(start) limit 代表获取多少个数据(limit)
		List<DBObject> list = c.find(query).skip(st).sort(new BasicDBObject("sort", 1)).limit(li).toArray();
		return list;
	}
	
	public int getAuthTotalCount(int state, DBCollection c){
		QueryBuilder queryBuilder = QueryBuilder.start();
		if(state==1){
			state = 0;
		}else if(state==0){
			state = 1;
		}
		// notEquals 表示不等于 is 表示等于
		DBObject query = queryBuilder.put("state").notEquals(state).get();
		
		int totalCount = c.find(query).length();
		return totalCount;
	}

	public boolean save(DBObject obj, DBCollection coll) {
		WriteResult wr = coll.save(obj, WriteConcern.NORMAL);
		int rs = wr.getN();
		if(rs==0){
			return true;
		}else{
			return false;
		}
	}

	public boolean update(DBObject obj, DBCollection c) {
		int auth_id = (Integer)obj.get("auth_id");
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("auth_id").is(auth_id).get();
		c.setWriteConcern(WriteConcern.NORMAL);
		WriteResult wr = c.update(query, obj);
		int rs = wr.getN(); 
		if(rs==1)
			return true;
		else
			return false;
	}
	
	public boolean delete(int id, DBCollection coll) {
		QueryBuilder queryBuilder = QueryBuilder.start();
		DBObject query = queryBuilder.put("auth_id").is(id).get();
		DBObject obj = coll.findAndRemove(query);//返回被执行删除的对象,没执行删除返回null
		if(null!=obj){
			return true;
		}else{
			return false;
		}
		/*WriteResult wr = coll.remove(query, WriteConcern.NORMAL);
		int rs = wr.getN(); //返回执行的条数，如果返回0 那么是没有执行操作，如果删除了2条 那么返回2
		System.out.println(rs);
		return true;*/
	}

}
